package com.css.dzj.dzjdzqs.modules.supervisionrecord.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjdzqs.common.audit.service.SysAuditService;
import com.css.dzj.dzjdzqs.common.util.BeanUtil;
import com.css.dzj.dzjdzqs.common.util.Constant;
import com.css.dzj.dzjdzqs.common.util.JavaBeanUtil;
import com.css.dzj.dzjdzqs.common.util.R;
import com.css.dzj.dzjdzqs.modules.supervisionrecord.entity.SupervisionEntity;
import com.css.dzj.dzjdzqs.modules.supervisionrecord.service.SupervisionService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 擅自公告监测记录Controller
 *
 * @author xgdou
 * @date 2020\3\18
 */
@RestController
@RequestMapping("/supervision")
public class SupervisionController {
    @Autowired
    private SupervisionService supervisionService;
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 新增
     */
    @PostMapping("/save")
    public R save(@RequestBody SupervisionEntity supervisionEntity) {
        String s;
        try {
            if (StringUtils.isEmpty(supervisionEntity)) {
                return R.error("公示通报信息不能为空");
            }
            s = JSON.toJSONString(supervisionEntity);
            supervisionService.add(supervisionEntity);
            sysAuditService.saveSuccessAudit("地震趋势添加成功", Constant.JCJL,Constant.PT,Constant.ADD,s,request);
            return R.ok();
        } catch (Exception e) {
            s = JSON.toJSONString(supervisionEntity);
            e.printStackTrace();
            sysAuditService.saveFailAudit("地震趋势添加失败",Constant.JCJL,Constant.PT,Constant.ADD,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 暂存
     */
    @PostMapping("/saveOnly")
    public R saveOnly(@RequestBody SupervisionEntity supervisionEntity) {
        String s;
        try {
            if (StringUtils.isEmpty(supervisionEntity)) {
                return R.error("公示通报信息不能为空");
            }
            supervisionService.addOnly(supervisionEntity);
            s = JSON.toJSONString(supervisionEntity);
            sysAuditService.saveSuccessAudit("地震趋势暂存成功", Constant.JCJL,Constant.PT,Constant.ZCADD,s,request);
            return R.ok();
        } catch (Exception e) {
            s = JSON.toJSONString(supervisionEntity);
            e.printStackTrace();
            sysAuditService.saveFailAudit("地震趋势暂存失败",Constant.JCJL,Constant.PT,Constant.ZCADD,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody SupervisionEntity supervisionEntity) {
        String s;
        try {
            SupervisionEntity old = supervisionService.findById(supervisionEntity.getId());
            BeanUtil.copyBeanNotEmptyToBean(supervisionEntity, old);
            old.setUpdateTime(new Date());
            supervisionService.update(old);
            s = JSON.toJSONString(supervisionEntity);
            sysAuditService.saveSuccessAudit("地震趋势修改成功", Constant.JCJL,Constant.PT,Constant.UPDATE,s,request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(supervisionEntity);
            sysAuditService.saveFailAudit("地震趋势修改失败",Constant.JCJL,Constant.PT,Constant.UPDATE,s,e,request);

            return R.error(e.getMessage());
        }
    }

    /**
     * 提交
     *
     * @param id
     * @return
     */
    @GetMapping("/submit")
    public R submit(@RequestParam String id) {
        try {
            supervisionService.submit(id);
            sysAuditService.saveSuccessAudit("地震趋势提交成功", Constant.JCJL,Constant.PT,Constant.TJ,id,request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("地震趋势修改失败",Constant.JCJL,Constant.PT,Constant.TJ,id,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable String id) {

        try {
            if (supervisionService.delete(id)) {
                sysAuditService.saveSuccessAudit("地震趋势删除成功", Constant.JCJL,Constant.PT,Constant.DEL,id,request);
                return R.ok("操作成功!");
            } else {
                return R.error("操作失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("地震趋势删除失败",Constant.JCJL,Constant.PT,Constant.DEL,id,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 多条件查询公示通报信息
     */
    @GetMapping("/publicityList")
    public R getXcbList(@RequestParam(name = "createUser", required = false) String createUser,
                        @RequestParam(name = "title", required = false) String title,
                        @RequestParam(name = "supervisionObjectName", required = false) String supervisionObjectName,
                        @RequestParam(name = "createUserUnit", required = false) String createUserUnit,
                        @RequestParam(name = "status", required = false) String status,
                        @RequestParam(name = "releaseStartTime", required = false) String releaseStartTime,
                        @RequestParam(name = "releaseEndTime", required = false) String releaseEndTime,
                        @RequestParam Integer pageSize, @RequestParam Integer pageNo) {
        String s;
        try {
            createUser = createUser.replaceAll(" ", "");
            title = title.replaceAll(" ", "");
            supervisionObjectName = supervisionObjectName.replaceAll(" ", "");
            createUserUnit = createUserUnit.replaceAll(" ", "");
            status = status.replaceAll(" ", "");
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<SupervisionEntity> supervisionResults = supervisionService.getSupervisionList(createUser, title, supervisionObjectName, createUserUnit, status, releaseStartTime, releaseEndTime, pageRequest);
            s = JSON.toJSONString(supervisionResults);
            sysAuditService.saveSuccessAudit("地震趋势查询成功", Constant.JCJL,Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", supervisionResults);
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("地震趋势查询失败",Constant.JCJL,Constant.PT,Constant.PAGE,s,e,request);
            return R.error(e.getMessage());
        }
    }

    @GetMapping("/export")
    public R export(HttpServletResponse response,
                    @RequestParam(required = false) String createUser,
                    @RequestParam(required = false) String title,
                    @RequestParam(required = false) String supervisionObjectName,
                    @RequestParam(required = false) String createUserUnit,
                    @RequestParam(required = false) String status,
                    @RequestParam(required = false) String releaseStartTime,
                    @RequestParam(required = false) String releaseEndTime) {
        String s;
        try {
            PageRequest pageRequest = PageRequest.of(0, 200);
            QueryResults<SupervisionEntity> supervisionList = supervisionService.getSupervisionList(createUser, title, supervisionObjectName, createUserUnit, status, releaseStartTime, releaseEndTime, pageRequest);
            List<SupervisionEntity> supervisionEntityList = supervisionList.getResults();
            supervisionService.exportDetailToExcel(response, supervisionEntityList);
            s = JSON.toJSONString(supervisionEntityList);
            sysAuditService.saveSuccessAudit("地震趋势导出成功", Constant.JCJL,Constant.PT,Constant.EXPORT,s,request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("地震趋势导出失败",Constant.JCJL,Constant.PT,Constant.EXPORT,s,e,request);
            return R.error(e.getMessage());
        }
    }
}
